<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>添加互动工作</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="100px" ref="editForm">
          <el-form-item label="互动名称：" prop="name">
            <el-input v-model="editForm.name" auto-complete="off" placeholder="填写互动名称"></el-input>
          </el-form-item>
          <el-form-item label="互动类型：" prop="name">
            <el-radio-group v-model="editForm.urgent">
              <el-radio label="0">一般</el-radio>
              <el-radio label="1">重要</el-radio>
              <el-radio label="2">紧急</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="限期时间：" prop="name">
            <el-date-picker type="date"
                            placeholder="限期时间"
                            v-model="editForm.end_time"
                            :picker-options="pickerOptions1"
                            value-format="yyyy-MM-dd"></el-date-picker>
            可不填
          </el-form-item>
          <el-form-item label="互动说明：" prop="name">
            <el-input v-model="editForm.descript" auto-complete="off" placeholder="填写互动说明" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="执行人员：" prop="num">
            <el-button @click.native="getMoreTeach(1)" size="small">选择执行人员（多选）</el-button>
            <el-button @click.native="userType=1,groupVisible=true" size="small">选择执行群组（多选）</el-button>
            <div>
              <el-tag
                  closable
                  :disable-transitions="false"
                  @close="handleCloseExamine(line, 1)"
                  class="mr-10"
                  v-for="(items,line) in dynamicTags"
                  :key="items.id"
              >{{ items.realname }}
              </el-tag>
            </div>
          </el-form-item>
          <el-form-item label="抄送人员：" prop="num">
            <el-button @click.native="getMoreTeach(2)" size="small">选择抄送人员（多选）</el-button>
            <el-button @click.native="userType=2,groupVisible=true" size="small">选择执行群组（多选）</el-button>
            <div>
              <el-tag
                  closable
                  :disable-transitions="false"
                  @close="handleCloseExamine(line, 2)"
                  class="mr-10"
                  v-for="(items,line) in readUserTags"
                  :key="items.id"
              >{{ items.realname }}
              </el-tag>
            </div>
          </el-form-item>
          <!--          <el-form-item label="执行群组：" prop="num">-->
          <!--            <el-button @click.native="getMoreTeach()" size="small">选择执行群组（多选）</el-button>-->
          <!--            <div>-->
          <!--              <el-tag-->
          <!--                  closable-->
          <!--                  :disable-transitions="false"-->
          <!--                  @close="handleCloseExamine(line)"-->
          <!--                  class="mr-10"-->
          <!--                  v-for="(items,line) in dynamicTags"-->
          <!--                  :key="items.id"-->
          <!--              >{{ items.realname }}-->
          <!--              </el-tag>-->
          <!--            </div>-->
          <!--          </el-form-item>-->
          <el-form-item label="附件：">
            <uploadFileModle number="20" @getFile="getFile" ref="uploadFileModle" :sycnStatus="true"
                             fileType="interaction_add"></uploadFileModle>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">返回</el-button>
        <el-button size="small" type="primary" @click.native="editSubmit">确定并保存</el-button>
      </div>
    </div>
    <!--    单选教职工审核人-->
    <MultipleTeacherModle :visible.sync="singleTeachVisible" @studentFn="studentFn"
                          :dynamicTagsArray="examine_user"></MultipleTeacherModle>
    <!--    选择群组-->
    <groupModule :visible.sync="groupVisible" @studentFn="studentFn"></groupModule>
  </div>
</template>

<script>
import {interactionAdd, interactionDetail, interactionEdit} from "@/api/admin/interaction"

export default {
  props: ['visible', 'workId'], // commoent 学生点评 collection 学生采集
  components: {},
  data() {
    return {
      singleTeachVisible: false,
      groupVisible: false,
      dialogVisible: false,
      examine_user: {}, // 审核人
      dynamicTags: [],
      editForm: {
        name: '', // 名称
        urgent: '0',
        descript: '',
        users: '', // 审核人
        end_time: '',
        appendix: '' // 附件
      }, // 编辑界面数据
      // 限制时间选择
      pickerOptions1:
          {
            disabledDate(time) {
              return time.getTime() < Date.now() - 8.64e7
            }
          },
      fileList: [],
      readUserTags: []
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.workId) {
          this.getDetail();
        }
      } else {
        this.editForm = {
          name: '', // 名称
          urgent: '0',
          descript: '',
          users: '', // 审核人
          end_time: '',
          appendix: '' // 附件
        }
        this.examine_user = {} // 审核人
        this.dynamicTags = []
        this.fileList = []
        this.readUserTags = []
      }
    }
  },
  created() {
  },
  methods: {
    // 打开审核人
    getMoreTeach(option) {
      this.userType = option
      let a = {}
      let b = this.userType == 1 ? JSON.parse(JSON.stringify(this.dynamicTags)) : JSON.parse(JSON.stringify(this.readUserTags));
      for (let item of b) {
        a[item.id] = item
      }
      // for (let item of this.dynamicTags) {
      //   a[item.id] = item
      // }
      this.examine_user = this.getJson(a)
      this.singleTeachVisible = true;
    },
    // 删除审核人
    handleCloseExamine(index, type) {
      if (type === 1) {
        this.dynamicTags.splice(index, 1)
      } else {
        this.readUserTags.splice(index, 1)
      }
    },
    // 审核人回执
    studentFn(option) {
      let list = []
      for (let i in option) {
        list.push(option[i])
      }
      if (this.userType == 1) {
        this.dynamicTags = this.getJson(list)
      } else if (this.userType == 2) {
        this.readUserTags = this.getJson(list)
      }
      console.log('提交的参数：', option, this.dynamicTags)
    },
    getDetail() {
      interactionDetail({id: this.groupId}).then(res => {
        console.log('res:', res)
        this.dynamicTags = this.getJson(res.vo.user_ids);
        this.editForm.name = res.vo.name;
      })
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    getFile(option) {
      console.log('收到附件数据：', option)
      this.fileList = this.getJson(option)
    },
    // 提交数据
    async editSubmit() {
      // 验证信息
      let dataOption = this.getJson(this.editForm)
      if (!dataOption.name) {
        this.$message.error('填写互动名称')
        return
      }
      if (!this.dynamicTags.length) {
        this.$message.error('请选择执行人员')
        return
      }
      let a = []
      for (let item of this.dynamicTags) {
        a.push(item.id)
      }
      dataOption.users = a.join(',')
      let b = []
      for (let item of this.readUserTags) {
        b.push(item.id)
      }
      dataOption.read_user = b.join(',')
      // name 	整型 	必填 	1 	- 	名称
      // user_ids 	整型 	必填 	1 	- 	用户id多个逗号隔开,
      let file = []
      for (let item of this.fileList) {
        let a = {}
        a.name = item.name
        a.src = item.src
        file.push(a)
      }

      dataOption.appendix = JSON.stringify(file)
      if (this.groupId) {
        dataOption.id = this.groupId
        interactionEdit(dataOption).then(res => {
          if (!res.code) {
            this.$message.success('编辑成功！')
            this.$emit('InitFn')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        interactionAdd(dataOption).then(res => {
          if (!res.code) {
            this.$message.success('编辑成功！')
            this.$emit('InitFn')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 50%;
  height: 550px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.hearder-search {
  display: flex;
  align-items: center;
}
</style>


